package org.wu.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.wu.reggie.common.R;
import org.wu.reggie.entity.Employee;
import org.wu.reggie.entity.Page;
import org.wu.reggie.exception.LogicException;
import org.wu.reggie.mapper.EmployeeMapper;
import org.wu.reggie.service.EmployeeService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;


    @Override
    public R<Employee> login(Employee employee) {

//        System.out.println(employee);
        // 查询是否有该员工信息
        Employee dbEmployee = employeeMapper.login(employee);
        // 如果为空说明没有该员工信息 无法进入后台
        if (dbEmployee == null){
            return R.error("用户名不存在,无法登录");
        }

        // 查询到员工信息 对比密码
        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!md5Password.equals(dbEmployee.getPassword())){
            return R.error("密码错误");
        }

        // 检查是否为禁用的用户
        if (dbEmployee.getStatus() == 0){
            return R.error("账号被禁用了");
        }

        // 登录成功
        return R.success(dbEmployee);
    }

    @Override
    public void save(Employee employee) {

        // 1 补全密码 状态 创建修改的时间
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setStatus(1);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());

        // 2 把 数据交给mapper插入
        employeeMapper.save(employee);
    }

    /**
     * 判断是否包含该用户名的员工
     * @param employee
     */
    @Override
    public void checkName(Employee employee) {


        String username = employee.getUsername();

        // 校验用户名是否相同
        List<Employee> employees = employeeMapper.list();

        for (Employee emp : employees) {
            if (emp.getUsername().equals(username)) {
                throw new LogicException(username+"用户名重复");
            }
        }


    }


    /**
     * 作用：员工列表分页
     * @param page 当前页
     * @param pageSize 当前页面大小
     * @param name 用户名
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        // 1 设置当前页与页面的大小
        PageHelper.startPage(page,pageSize);
        // 2 查询数据
        List<Employee> employeeList = employeeMapper.findByName(name);
        // 3 创建PageInfo对象 把list集合传入
        PageInfo<Employee> pageInfo = new PageInfo(employeeList);
        // 4 把pageinfo的信息转移到page对象中
        Page<Employee> pageResult = new Page<>(
                employeeList,
                pageInfo.getTotal(),
                pageInfo.getPageSize(),
                pageInfo.getPageNum());
        return pageResult;
    }

    @Override
    public void update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }

    // 根据id查找员工
    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }
}
